//
//  MYGFindController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/11.
//

import UIKit
import QMUIKit
import JXSegmentedView

class MYGFindController: MYGBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        view.addSubview(stackView)
        view.addSubview(segmentView)
        view.addSubview(lineView)
        view.addSubview(containView)
        
        stackView.addArrangedSubviews([backButton, searchTextField, memberButton, addButton])
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(18)
            make.top.equalToSuperview().offset(statusBarHeight)
            make.height.equalTo(navHeight)
        }
        
        searchTextField.snp.makeConstraints { make in
            make.height.equalTo(30)
        }
        
        segmentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(stackView.snp.bottom)
            make.height.equalTo(47)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(14)
            make.bottom.equalTo(segmentView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        containView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(segmentView.snp.bottom)
        }
        
        backButton.setContentHuggingPriority(.required, for: .horizontal)
        backButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        memberButton.setContentHuggingPriority(.required, for: .horizontal)
        memberButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        addButton.setContentHuggingPriority(.required, for: .horizontal)
        addButton.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    
    // MARK: - lazy load
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.spacing = 10
        stackV.alignment = .center
        return stackV
    }()
    
    private lazy var backButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/back"), for: .normal)
        btn.imageView?.tintColor = .rgba(51, 51, 51, 1)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var searchTextField: UITextField = {
        let tf = UITextField()
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51)
        tf.backgroundColor = .rgba(243, 243, 243, 1)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 15
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.image(UIImage(named: "Common/search"))
            confer.text(" 搜索")?.font(.regular(14)).textColor(.rgba(142, 142, 147, 1))
        })
        tf.textAlignment = .center
        tf.addTarget(self, action: #selector(textValueChanged), for: .editingChanged)
        tf.delegate = self
        return tf
    }()
    
    private lazy var searchButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Common/search"), for: .normal)
        btn.setTitle("搜索", for: .normal)
        btn.setTitleColor(.rgba(142, 142, 147, 1), for: .normal)
        btn.titleLabel?.font = .regular(14)
        btn.imagePosition = .left
        btn.spacingBetweenImageAndTitle = 3
        btn.backgroundColor = .rgba(243, 243, 243, 1)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 15
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var memberButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Find/member"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var addButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Find/add"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var segmentView: JXSegmentedView = {
        let view = JXSegmentedView()
        view.dataSource = dataSource
        view.listContainer = containView
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 16
        indicator.indicatorHeight = 3
        indicator.indicatorColor = .red
        indicator.indicatorPosition = .bottom
        
        view.indicators = [indicator]
        return view
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
    
    private lazy var dataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = ["关注", "好友", "推荐"]
        dataSource.titleNormalFont = .regular(14)
        dataSource.titleNormalColor = .rgba(118, 118, 118, 1)
        dataSource.titleSelectedFont = .regular(16)
        dataSource.titleSelectedColor = .red
        return dataSource
    }()
    
    private lazy var containView: JXSegmentedListContainerView = {
        let view = JXSegmentedListContainerView(dataSource: self)
        
        return view
    }()
    
    private lazy var careVc: MYGFindListController = {
        let vc = MYGFindListController()
        vc.type = 2
        return vc
    }()
    
    private lazy var friendVc: MYGFindListController = {
        let vc = MYGFindListController()
        vc.type = 3
        return vc
    }()
    
    private lazy var recommendVc: MYGFindListController = {
        let vc = MYGFindListController()
        vc.type = 4
        return vc
    }()
    
    override var hideNavigationController: Bool {
        true
    }
}

extension MYGFindController: UITextFieldDelegate {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == backButton {
            self.navigationController?.popViewController()
        } else if btn == addButton {
            if ((MYGUserManager.getUser()?.doctor.name.isEmpty) == true) || MYGUserManager.getUser()?.doctor.gender == 0  || ((MYGUserManager.getUser()?.doctor.identity.isEmpty) == true) {
                let vc = MYGBaseInfoController()
                self.navigationController?.pushViewController(vc)
                return
            }
            let vc = MYGFindSendController()
            self.navigationController?.pushViewController(vc)
        } else if btn == memberButton {
            let vc = MYGFindListController()
            vc.type = 1
            vc.title = "我的动态"
            vc.hidNav = false
            self.navigationController?.pushViewController(vc)
        }
    }
    
    @objc
    private func textValueChanged(_ textField: UITextField) {
        let text = textField.text ?? ""
        careVc.keyword = text
        friendVc.keyword = text
        recommendVc.keyword = text
    }
    
    func textFieldDidChangeSelection(_ textField: UITextField) {
        let text = textField.text ?? ""
        careVc.keyword = text
        friendVc.keyword = text
        recommendVc.keyword = text
    }
}

extension MYGFindController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        dataSource.titles.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        [careVc, friendVc, recommendVc][index]
    }
    
    
}
